﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using PRDP.Tools;
using System.IO;
using System.Collections.Generic;
using System.Text;
using PRDP.Search;

public partial class detail : System.Web.UI.Page
{
    //当前信息所属分类
    string typeID = "1";
    //分类名
    string typeName = "未知";
    //
    string highLightWord = "";
    //当前信息的D
    string id;
    //所有要显示的字段
    HashSet<string> displayList;
    //当前要显示的所有信息
    Hashtable detailInformation;
    //当前分类对应的数据表
    string table;
    //页眉显示最多的搜索类型数目
    //int maxSearchTypeNum = 10;

    protected void Page_Load(object sender, EventArgs e)
    {
        if (Request.QueryString["id"] == null || Request.QueryString["id"] == "" || Request.QueryString["typeid"] == null || Request.QueryString["typeid"] == "")
            Response.Redirect("admin/msg.aspx?msg=参数错误，请返回！&gourl=../search.aspx", true);

        this.typeID = Request.QueryString["typeid"];
        this.id = Request.QueryString["id"];
        this.highLightWord = Request.QueryString["highlight"];
        //预处理
        preProcess();
        //详细内容
        generateRecordHTML();
        //输出客户端JS

    }

    public void preProcess()
    {
        //查询当前分类下相关信息
        Hashtable row = MyDBHandler.getOne("select Tables,DetailFields,CnName from Type where ID = " + this.typeID);
        this.table = row["Tables"].ToString();
        this.typeName = row["CnName"].ToString();
        this.displayList = StringHelper.SplitStringToSet(row["DetailFields"].ToString(), ",");
        //详细显示字段如果没有原网址则加上
        if (row["DetailFields"].ToString().ToLower().IndexOf("url") == -1)
        {
            row["DetailFields"] = row["DetailFields"].ToString() + ",Url";
        }
        //详细显示字段如果没有标题则加上
        if (row["DetailFields"].ToString().ToLower().IndexOf("title") == -1)
        {
            row["DetailFields"] = row["DetailFields"].ToString() + ",Title";
        }
        this.detailInformation = MyDBHandler.getOneWithLowerCase("select " + row["DetailFields"].ToString() + " from " + this.table + " where ID=" + this.id);
    }

    public void generateRecordHTML()
    {
        List<Hashtable> fieldAndComment = MyDBHandler.getAllList("select isc.COLUMN_NAME,p.value from  information_schema.columns isc,sys.extended_properties p where isc.TABLE_NAME='" + this.table + "' and isc.ORDINAL_POSITION=p.minor_id and major_id=OBJECT_ID('" + this.table + "')");
        foreach (Hashtable record in fieldAndComment)
        {
            if (this.displayList.Contains(record["COLUMN_NAME"].ToString()))
            {
                laDetailInformationHTML.Text += this.RangerFieldUserControl("~/userControl/detailRecord", record["value"].ToString(), MySearcher.getInstance().getHignligthString(record["COLUMN_NAME"].ToString(), this.highLightWord, this.detailInformation[record["COLUMN_NAME"].ToString().ToLower()].ToString()));
            }
            if (record["COLUMN_NAME"].ToString().ToLower() == "title")
            {
                try
                {
                    laTitle.Text = this.detailInformation["title"].ToString() + " <a href=" + this.detailInformation["url"].ToString() + " target=_blank>[查看原文]</a>";
                }
                catch (NullReferenceException)
                {
                    //
                }
            }
                
        }
    }

    /// <summary>
    /// 输出每个记录项
    /// </summary>
    public string RangerFieldUserControl(string controlName, string fieldName, string fieldStr)
    {
        StringBuilder build = new StringBuilder();
        HtmlTextWriter htmlWriter = new HtmlTextWriter(new StringWriter(build));

        UserControl uc = new UserControl();
        Control ctrl = uc.LoadControl(controlName + ".ascx");

        Label fieldNameControl = (Label)(ctrl.FindControl("laFieldName"));
        fieldNameControl.Text = fieldName;

        Label fieldStrControl = (Label)(ctrl.FindControl("laFieldStr"));
        fieldStrControl.Text = fieldStr;

        htmlWriter.Flush();
        string result;
        try
        {
            ctrl.RenderControl(htmlWriter);
        }
        catch (Exception)
        {
            
        }
        finally
        {
            htmlWriter.Flush();
            result = build.ToString();
        }
        return result;
    }

}
